package com.crrc.yzt.demo.vm

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.crrc.yzt.demo.repository.CarRepository
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlin.random.Random
import kotlin.random.nextInt

class CarValueVM : ViewModel() {

    private val _carValueListStateFlow: MutableStateFlow<List<String>?> = MutableStateFlow(null)
    val carValueListStateFlow = _carValueListStateFlow.asStateFlow()


    fun getCarValueList(index:Int){

        viewModelScope.launch {

            repeat(Int.MAX_VALUE){

                val random = Random.nextInt(IntRange(0,9))

                Log.i("sss", "getCarValueList: random = $random")

                val list = CarRepository.getCarValueList(index,random)

                _carValueListStateFlow.emit(list)

                delay(1000)

            }

        }

    }





}